package com.caigou.component;

import com.caigou.bean.cms.entity.Article;
import com.caigou.bean.cms.entity.ChannelData;
import com.caigou.service.ArticleService;
import com.caigou.service.ChannelDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @FileName: ArticleCommponent
 * @Author: 小飞
 * @Date: 2022/2/18 0018
 * @Description: 文章类公共方法
 */
@Slf4j
@Component
public class ArticleComponent {

    @Resource
    private HtmlDetailBuilderComponent htmlDetailBuilderComponent;

    @Resource
    private ArticleService articleService;
    @Resource
    private ChannelDataService channelDataService;

    /**
     * 更新文章URL方法
     * @param articleId
     */
    public void updateArticleUrl(String articleId){
        Article article = articleService.loadArticle(articleId);
        if (null != article){
            log.info("当前文章未生成过URL，生成文章的URL articleId={}", articleId);
            ChannelData channelData = channelDataService.loadLastChannelData(articleId);
            if (null == channelData) {
                htmlDetailBuilderComponent.buildArticleHtml(articleId, "999");
            } else {
                htmlDetailBuilderComponent.buildArticleHtml(articleId, channelData.getPlateId());
            }
        } else {
            log.info("未获取到文章数据，不进行文章URL生成 articleId={}", articleId);
        }
    }
}
